package com.service.task;

import java.util.HashMap;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.dao.task.AtInforMapper;
import com.dao.task.AtProjectMapper;
import com.dao.task.TaskMapper;
import com.model.task.AtInfor;
import com.model.task.AtProject;

@Service
public class TaskSurpviseService {
	static final Logger logger = LoggerFactory.getLogger(TaskSurpviseService.class);

	@Autowired
	private AtInforMapper atInforMapper;
	@Autowired
	private TaskMapper taskMapper;
	@Autowired
	private AtProjectMapper atProjectMapper;

	@Transactional
	public int addProject(String exeUsers, AtInfor atInfor) {
		int i = 0;
		JSONArray jsonArray = JSON.parseArray(exeUsers);
		HashMap<String, Object> hashMap = new HashMap<>();
		hashMap.put("inf_id", atInfor.getInfId());
		i = taskMapper.deleteProject(hashMap);
		for (Object obj : jsonArray) {
			JSONObject jsonObject = (JSONObject) obj;
			AtProject model = new AtProject();
			model.setInfId(atInfor.getInfId());
			model.setProExecuteuserid(jsonObject.getString("id"));
			model.setProExecuteusername(jsonObject.getString("name"));
			model.setProCreatetime(atInfor.getInfCreattime());
			model.setProCreateorid(atInfor.getInfCreatorid());
			model.setProCreateorname(atInfor.getInfCorpname());
			i += atProjectMapper.insert(model);
		}
		return i;
	}

	@Transactional
	public int delSurpvise(String infID, String proExecuteuserid, String proId) {
		int i = 0;
		atProjectMapper.deleteByPrimaryKey(infID, proExecuteuserid, proId);
		HashMap<String, Object> hashMap = new HashMap<>();
		hashMap.put("inf_id", infID);
		hashMap.put("proExecuteuserid", infID);
		taskMapper.deleteProjectResult(hashMap);
		return i;
	}

	@Transactional
	public int delMeet(String infID) {
		int i = 0;
		i = atInforMapper.deleteByPrimaryKey(infID);
		HashMap<String, Object> hashMap = new HashMap<>();
		hashMap.put("inf_id", infID);
		taskMapper.deleteProject(hashMap);
		taskMapper.deleteProjectResult(hashMap);
		return i;
	}
}
